package com.mb.app.controller;

import com.mb.app.domain.TbChannelConfig;
import com.mb.app.domain.TbUserInfo;
import com.mb.app.service.ITbChannelConfigService;
import com.mb.app.service.ITbUserInfoService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author zwl
 * @program
 * @description 用户查询管理
 * @date 2019-03-21 16:58
 **/
@Controller
@RequestMapping("/app/userQuery")
@Slf4j
public class UserQueryController extends BaseController {

    @Autowired
    private ITbUserInfoService userInfoService;

    @Autowired
    private ITbChannelConfigService channelConfigService;

    @GetMapping("/index")
    String index(){
        return "/app/userQuery/userQuery_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String regTime = (String) params.get("regTime");
        if (StringUtils.isNotEmpty(regTime)) {
            params.put("startTime", DateUtils.parse(regTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",regTime.substring(13,23)+" 23:59:59");
        }
        params.put("regChannelId",getUserId());
        Query query = new Query(params);
        List<TbUserInfo> list = null;
        int total = userInfoService.countChannel(query);
        //根据用户id得到渠道编号，根据渠道id 查询对应的deduct_rate
        log.info("total============================"+total);
        TbUserInfo tbUserInfo = userInfoService.getDtl(getUserId());
        if(null != tbUserInfo && null != tbUserInfo.getRegChannelId()) {
            params.put("regChannelId",tbUserInfo.getRegChannelId());
            list = userInfoService.queryListChannel(query);
            TbChannelConfig tbChannelConfig = channelConfigService.getDtl(tbUserInfo.getRegChannelId());
            if (null != tbChannelConfig && null !=  tbChannelConfig.getDeductRate()) {
                //设置小数位数，第一个变量是小数位数，第二个变量是取舍方法(四舍五入)
                BigDecimal bd=tbChannelConfig.getDeductRate().setScale(0, BigDecimal.ROUND_HALF_UP);
                //转化为字符串输出
                double deductRate = Double.parseDouble(bd.toString());
                log.info("deductRate======================="+deductRate);
                double tt = total * (1-deductRate/100);
                log.info("tt============================"+tt);
                total = (new Double(tt)).intValue();
                log.info("total============================"+total);
            }
        }
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }


}
